package com.marvinlabs.widget.slideshow.transition;

import com.marvinlabs.widget.slideshow.SlideShowView;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;

/**
 * A transition maker to slide out left and zoom in
 * <p/>
 * Created by Vincent Mimoun-Prat @ MarvinLabs on 28/05/2014.
 */
public class SlideAndZoomTransitionFactory extends BaseTransitionFactory {

    //==============================================================================================
    // GENERAL METHODS
    //==

    /**
     * Default constuctor
     */
    public SlideAndZoomTransitionFactory() {
        super();
    }

    /**
     * Constructor
     *
     * @param duration Duration for the transition in ms
     */
    public SlideAndZoomTransitionFactory(long duration) {
        super(duration);
    }

    /**
     * Constructor
     *
     * @param duration The duration for the transition in ms
     * @param interpolator The kind of interpolator we need
     */
    public SlideAndZoomTransitionFactory(long duration, Animator.TimelineCurve interpolator) {
        super(duration, interpolator);
    }

    //==============================================================================================
    // INTERFACE IMPLEMENTATION: SlideTransitionFactory
    //==

    @Override
    public AnimatorValue getInAnimator(Component target, SlideShowView parent, int fromSlide, int toSlide) {
        target.setAlpha(0);
        target.setScaleX(1);
        target.setScaleY(1);
        target.setTranslationX(0);
        target.setTranslationY(0);
        target.setRotation(0);

        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(getDuration() / 2);
        animator.setDelay(getDuration() / 2);
        animator.setCurve(getInterpolator());
        animator.setValueUpdateListener((animatorValue, value) -> {
            target.setScale(0.5f + 0.5f * value, 0.5f + 0.5f * value);
            target.setAlpha(value);
            target.setTranslationX(parent.getWidth() - parent.getWidth() * value);
        });

        return animator;
    }

    @Override
    public AnimatorValue getOutAnimator(Component target, SlideShowView parent, int fromSlide, int toSlide) {
        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(getDuration() / 2);
        animator.setDelay(getDuration() / 2);
        animator.setCurve(getInterpolator());
        animator.setValueUpdateListener((animatorValue, value) -> {
            target.setScale(1f - 0.5f * value, 1f - 0.5f * value);
            target.setAlpha(1 - value);
            target.setTranslationX(-parent.getWidth() * value);
        });

        return animator;
    }

}
